﻿//
// Copyright (c) 2010
// by Ashish Agrawal and Chris Paterra
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using DotNetNuke.Web.Mvp;
using SimpleBlog.Components.Data;
using SimpleBlog.Components.Controllers;
using SimpleBlog.Components.Models;
using SimpleBlog.Components.Views;
using SimpleBlog.Components.Common;
using System.Web.UI.WebControls;
using SimpleBlog.Components.Entities;

namespace SimpleBlog.Components.Presenters
{

	/// <summary>
	/// 
	/// </summary>
	public class EntriesPresenter : ModulePresenter<IEntriesView, EntriesModel>
	{

		#region Private Members

		protected ISimpleBlogController Controller { get; private set; }

		#endregion

		#region Constructor

		/// <summary>
		/// 
		/// </summary>
		/// <param name="view"></param>
		public EntriesPresenter(IEntriesView view)
			: this(view, new SimpleBlogController(new SqlDataProvider()))
		{
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="view"></param>
		/// <param name="controller"></param>
		public EntriesPresenter(IEntriesView view, ISimpleBlogController controller)
			: base(view)
		{
			if (view == null)
			{
				throw new ArgumentException(@"View is nothing.", "view");
			}

			if (controller == null)
			{
				throw new ArgumentException(@"Controller is nothing.", "controller");
			}

			Controller = controller;
		}

		#endregion

		#region Events

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void ItemDataBound(object sender, UIListEventArgs<HyperLink, Image, HyperLink, Literal, EntryInfo, HyperLink, Literal> e)
		{
			e.EditLink.NavigateUrl = Links.EditEntryLink(ModuleContext.TabId, ModuleContext.ModuleId, e.ObjEntry.EntryId);
			e.TitleLink.NavigateUrl = Links.ViewControlLink(ModuleContext.TabId, ModuleContext.ModuleId, Constants.PageScope.Entry, e.ObjEntry.EntryId);
			e.CommentLink.NavigateUrl = ModuleContext.NavigateUrl(ModuleContext.TabId, "ViewEntry", false, "EntryID=" + e.ObjEntry.EntryId);
			//e.CommentLink.Text = DotNetNuke.Services.Localization.Localization.GetString("Comments", LocalResourceFile) +  " (" + e.ObjEntry.Comments + ")";

			e.DateLiteral.Text = @"<h4>" + e.ObjEntry.PublishOnDate.ToShortDateString() + @"</h4>";

			if (e.ObjEntry.Summary == DotNetNuke.Common.Utilities.Null.NullString)
			{
				if (e.ObjEntry.Content.Length > 2000)
				{
					e.Summary.Text = Server.HtmlDecode(e.ObjEntry.Content).Substring(0, 2000) + @"...";
				}
				else
				{
					e.Summary.Text = Server.HtmlDecode(e.ObjEntry.Content);
				}
			}
			else
			{
				e.Summary.Text = Server.HtmlDecode(e.ObjEntry.Summary);
			}
		}

		/// <summary>
		/// 
		/// </summary>
		protected override void OnLoad()
		{
			base.OnLoad();

			View.ItemDataBound += ItemDataBound;
			View.Model.IsEditable = ModuleContext.IsEditable;

			var colEntries = Controller.GetPublishedModuleEntries(ModuleContext.ModuleId, 5, 0);
			View.Model.PublishedEntries = colEntries;
			View.Refresh();
		}

		#endregion

	}
}